package cn.hrlly.tao2shou.view.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.other.MyApplication;
import cn.hrlly.tao2shou.view.base.BaseFragment;
import cn.hrlly.tao2shou.view.fragment.HomePageFragment;
import cn.hrlly.tao2shou.view.fragment.MyFragment;
import cn.hrlly.tao2shou.view.fragment.PublishFragment;
import cn.hrlly.tao2shou.view.fragment.RequireFragment;

public class MainActivity extends BaseFragment implements View.OnClickListener {

    @Bind(R.id.homePageTab)
    LinearLayout homePageTab;
    @Bind(R.id.requireTab)
    LinearLayout requireTab;
    @Bind(R.id.publishTab)
    LinearLayout publishTab;
    @Bind(R.id.myTab)
    LinearLayout myTab;
    @Bind(R.id.btnHomePageTab)
    ImageButton btnHomePageTab;
    @Bind(R.id.btnRequireTab)
    ImageButton btnRequireTab;
    @Bind(R.id.btnPublishTab)
    ImageButton btnPublishTab;
    @Bind(R.id.btnMyTab)
    ImageButton btnMyTab;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initEvent();
        setSelect(0);
    }

    //初始化控件
    public void initView(){
        mFragments = new ArrayList<Fragment>();
        Fragment mTab01 = new HomePageFragment();
        Fragment mTab02 = new RequireFragment();
        Fragment mTab03 = new PublishFragment();
        Fragment mTab04 = new MyFragment();
        mFragments.add(mTab01);
        mFragments.add(mTab02);
        mFragments.add(mTab03);
        mFragments.add(mTab04);

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
        {
            @Override
            public int getCount()
            {
                return mFragments.size();
            }
            @Override
            public Fragment getItem(int arg0)
            {
                return mFragments.get(arg0);
            }
        };
        viewPager.setAdapter(mAdapter);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                int currentItem = viewPager.getCurrentItem();
                setTab(currentItem);
            }
            @Override
            public void onPageSelected(int position) {

            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    //tab的点击事件
    public void initEvent(){
        homePageTab.setOnClickListener(this);
        requireTab.setOnClickListener(this);
        publishTab.setOnClickListener(this);
        myTab.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.homePageTab:
                setSelect(0);
                break;
            case R.id.requireTab:
                setSelect(1);
                break;
            case R.id.publishTab:
                setSelect(2);
                break;
            case R.id.myTab:
                setSelect(3);
                break;

            default:
                break;
        }
    }

    //viewPager选择
    public void setSelect(int i)
    {
        setTab(i);
        viewPager.setCurrentItem(i);
    }

    //选择tab
    public void setTab(int i)
    {
        resetImgs();
        // 设置图片为亮色
        // 切换内容区域
        switch (i)
        {
            case 0:
                btnHomePageTab.setImageResource(R.mipmap.ic_launcher);
                break;
            case 1:
                btnRequireTab.setImageResource(R.mipmap.ic_launcher);
                break;
            case 2:
                btnPublishTab.setImageResource(R.mipmap.ic_launcher);
                break;
            case 3:
                btnMyTab.setImageResource(R.mipmap.ic_launcher);
                break;
        }
    }

    //切换图片至暗色
    private void resetImgs()
    {
        btnHomePageTab.setImageResource(R.mipmap.user);
        btnRequireTab.setImageResource(R.mipmap.user);
        btnPublishTab.setImageResource(R.mipmap.user);
        btnMyTab.setImageResource(R.mipmap.user);
    }
}
